{layout name="layout" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-header layuiadmin-card-header-auto"></div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-role" data-type="batchdel"> 删除 </button>
        <button class="layui-btn layuiadmin-btn-role" data-type="add"> 添加 </button>
      </div>
      <div id="zbc-user-permission" lay-filter="zbc-user-permission"></div>
    </div>
  </div>
</div>
<script type="text/html" id="table-content-custom">
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script>
  layui
    .config({
      base: "/static/layuiadmin/" //静态资源所在路径
    })
    .extend({
      index: "lib/index", //主入口模块
      treetable: "../lib/treetable/treetable"
    })
    .use(["index", "useradmin", "table", "treetable"], function () {
      var $ = layui.$,
        form = layui.form,
        table = layui.table,
        admin = layui.admin;
      var treetable = layui.treetable;

      // 渲染表格
      var renderTable = function () {
        layer.load(2);
        treetable.render({
          treeColIndex: 1,
          treeSpid: 0,
          treeIdName: "id",
          treePidName: "pid",
          treeDefaultClose: false,
          treeLinkage: false,
          elem: "#zbc-user-permission",
          url: "/admin/permission/list",
          cols: [
            [
              { type: "checkbox" },
              { field: "name", title: "名称" },
              { field: "key", title: "标识" },
              { field: "url", title: "地址" },
              { field: "level", title: "层级" },
              { field: 'type', title: '类型', templet: function (data) { if (data.type == 1) { return '菜单' } else if (data.type == 2) { return '按钮' } else if (data.type == 0) { return '目录' } } },
              { field: "order", title: "排序" },
              {
                title: "操作",
                width: 150,
                align: "center",
                fixed: "right",
                toolbar: "#table-content-custom"
              }
            ]
          ],
          done: function () {
            layer.closeAll("loading");
          },
          fail: function (xhr) {
            if (xhr.status == 401) {
              layer.alert('权限改变,系统触发刷新', function (index) {
                layer.close(index);
                parent.location.href = '/admin/home'
              })
            }
          }
        });

        //监听工具条
        table.on("tool(zbc-user-permission)", function (obj) {
          var data = obj.data;
          if (obj.event === "del") {
            layer.confirm("确定删除吗？", function (index) {
              admin.req({
                url: '/admin/permission/delete',
                method: 'post',
                data: data,
                success: function (result) {
                  if (result.data) {
                    renderTable();
                    layer.msg("已删除");
                  } else {
                    layer.msg(result.msg);
                  }
                }
              })
            });
          } else if (obj.event === "edit") {
            layer.open({
              type: 2,
              title: '编辑权限',
              content: "/admin/permission/edit?id=" + data.id,
              maxmin: true,
              area: ['900px', '620px'],
              btn: ["确定", "取消"],
              yes: function (index, layero) {
                var iframeWindow = window["layui-layer-iframe" + index],
                  submitID = 'zbc-user-permission-form-submit',
                  submit = layero
                    .find("iframe")
                    .contents()
                    .find("#" + submitID);

                iframeWindow.layui.form.on("submit(" + submitID + ")", function (data) {
                  var field = data.field; //获取提交的字段
                  if (iframeWindow.ue) {
                    field.content = iframeWindow.ue.getContent();
                  }
                  admin.req({
                    url: '/admin/permission/edit',
                    data: field,
                    method: 'post',
                    success: function (result) {
                      if (result.data) {
                        renderTable();
                        layer.msg("已完成");
                        layer.close(index); //关闭弹层
                      } else {
                        layer.msg(result.msg, { icon: 4 });
                      }
                    }
                  })
                });
                submit.trigger("click");
              }
            });
          }
        });
        //事件
        var active = {
          batchdel: function () {
            var checkStatus = table.checkStatus("zbc-user-permission"),
              checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
              return layer.msg("请选择数据");
            }

            layer.confirm("确定删除吗？", function (index) {

              admin.req({
                url: '/admin/permission/delete',
                data: {
                  list: $.map(checkData, function (v) {
                    return v["id"];
                  })
                },
                method: 'post',
                success: function (result) {
                  if (result.data) {
                    renderTable();
                    layer.msg("已删除");
                    layer.close(index);
                  } else {
                    layer.msg(result.msg);
                  }
                }
              })
            });
          },
          add: function () {
            layer.open({
              type: 2,
              title: "添加权限",
              content: "/admin/permission/add",
              area: ['900px', '620px'],
              btn: ["确定", "取消"],
              yes: function (index, layero) {
                var iframeWindow = window["layui-layer-iframe" + index],
                  submit = layero
                    .find("iframe")
                    .contents()
                    .find("#zbc-user-permission-form-submit");
                //监听提交
                iframeWindow.layui.form.on(
                  "submit(zbc-user-permission-form-submit)",
                  function (data) {
                    var field = data.field; //获取提交的字段
                    layer.msg(JSON.stringify(field))
                    admin.req({
                      url: '/admin/permission/add',
                      method: 'post',
                      data: field,
                      success: function (result) {
                        if (result.data) {
                          renderTable();
                          layer.msg('已完成');
                          layer.close(index);
                        }
                      }
                    })
                  }
                );
                submit.trigger("click");
              }
            });
          }
        };

        $(".layui-btn.layuiadmin-btn-role").on("click", function () {
          var type = $(this).data("type");
          active[type] ? active[type].call(this) : "";
        });
      };

      renderTable();
    });
</script>